function modeSort() {
    dctrl.modOn();
    jQuery("#edit_top_menu_trigger").show();
    jQuery("#sort_top_menu_trigger").hide();
}
  
function modeEdit() {
  dctrl.modOff();
  jQuery("#edit_top_menu_trigger").hide();
  jQuery("#sort_top_menu_trigger").show();
}
  
function startEditTopMenu() {
  var div = jQuery("#top_menu_edit_popup");
  var action = jQuery("#top_menu_settiings_action");
  
  if ( div.get(0) && action.get(0) ) {
    jQuery.get(action.get(0).href, function(data) {
      jQuery("#top_menu_settings").html(data);
      
      dctrl.backgroundOn();
      div.show();
    });
  }
}

function closeEditTopMenu() {
  var div = jQuery("#top_menu_edit_popup");

  if ( div.get(0) ) {
    div.hide();
    dctrl.backgroundOff();
  }
}
function saveEditTopMenu() {
  var div = jQuery("#top_menu_edit_popup");

  if ( !div.get(0) ) {
    return;
  }

  var position = new Array();
  div.find("div.js_linkdiv").each( function(i) {
    position.push(this.id);
  });
    
  position_string = position.join(",");

  var itemsString = jQuery("#save_top_menu_form").serialize();
  var action = jQuery("#save_top_menu_form").get(0).action;

  jQuery.get(action, { "items": itemsString, "position": position_string }, function(data) {
    if ( "error" == data ) {
      alert("Ошибка при сохранении формы.");
      return;
    }

    jQuery("#top_menu_content").html(data);

    closeEditTopMenu();
  });
}
